package cn.edu.lzu.unitweb.controller;

import cn.edu.lzu.common.assist.DataPage;
import cn.edu.lzu.common.assist.ResponseResult;
import cn.edu.lzu.entity.ExamineItem;
import cn.edu.lzu.entity.Examiner;
import cn.edu.lzu.entity.FillLabItem;
import cn.edu.lzu.entity.LabExamineClause;
import cn.edu.lzu.service.ExamineItemService;
import cn.edu.lzu.service.LabExamineClauseService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.support.DefaultSubjectContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.xml.ws.Response;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @PackageName: cn.edu.lzu.unitweb.controller
 * @ClassName: ItemController
 * @Description: TODO
 * @Author: 张琦[QQ:3098086691]
 * @Date: 2019/12/8 19:45
 * @Version: 1.0
 */
@Controller
public class ExamineItemController {
    @Resource
    private ExamineItemService examineItemService;
    @Resource
    private LabExamineClauseService labExamineClauseService;

    /**
     * @Author: 张琦[QQ:3098086691]
     * @Description: TODO 获取一页‘检查项目’数据
     * @Date: 2019/12/9 14:10
     * @Param: []
     * @Return: cn.edu.lzu.common.assist.DataPage
     **/
    @ResponseBody
    @RequestMapping("/getExamineItems")
    public DataPage getItemListByUnitId(int currentPageNum, int pageSize) {
        Session session = SecurityUtils.getSubject().getSession();
        Examiner examiner = (Examiner) ((PrincipalCollection) session.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY)).getPrimaryPrincipal();
        DataPage itemPage = examineItemService.getExamineItemPageByUnitId(examiner.getUnitId(), currentPageNum, pageSize);
        System.out.println(itemPage.getData());
        return itemPage;
    }

    /**
     * @Author: 张琦[QQ:3098086691]
     * @Description: TODO 添加新项目
     * @Date: 2019/12/10 10:48
     * @Param: [examineItem]
     * @Return: cn.edu.lzu.common.assist.ResponseResult
     **/
    @ResponseBody
    @RequestMapping("/executeAddExamineItem")
    public ResponseResult addExamineItem(ExamineItem examineItem, Integer typeLevel1, Integer typeLevel2, Integer typeLevel3, Integer typeLevel4) {
        //public ResponseResult addExamineItem(ExamineItem examineItem, Map ...maps){
        System.out.println("ExamineItem: " + examineItem + "\t department:" + typeLevel1 + " typeLevel2:" + typeLevel2 + " typeLevel3:" + typeLevel3 + " typeLevel4:" + typeLevel4);
        if (typeLevel4 != null)
            examineItem.setItemTypeId(typeLevel4);
        else if (typeLevel3 != null)
            examineItem.setItemTypeId(typeLevel3);
        else if (typeLevel2 != null)
            examineItem.setItemTypeId(typeLevel2);
        else
            examineItem.setItemTypeId(typeLevel1);
        Session session = SecurityUtils.getSubject().getSession();
        Examiner examiner = (Examiner) ((PrincipalCollection) session.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY)).getPrimaryPrincipal();
        examineItem.setUnitId(examiner.getUnitId());
        ResponseResult responseResult = examineItemService.executeAddExamineItem(examineItem);
        return responseResult;
    }


    /**
     * @Author: 张琦[QQ:3098086691]
     * @Description: TODO 根据项目编号单独删除一个项目：点击‘操作’栏中的‘删除’按钮
     * @Date: 2019/12/9 15:11
     * @Param: [examineItemId]
     * @Return: cn.edu.lzu.common.assist.ResponseResult
     **/
    @ResponseBody
    @RequestMapping("/deleteExamineItemById")
    public ResponseResult deleteExamineItemById(int examineItemId) {
        System.out.println("examineItemId:" + examineItemId);
        ResponseResult responseResult = examineItemService.deleteExamineItemById(examineItemId);
        return responseResult;
    }

    /**
     * @Author: 张琦[QQ:3098086691]
     * @Description: TODO 批量删除体检项目
     * @Date: 2019/12/9 21:44
     * @Param: [examineItemIds]
     * @Return: cn.edu.lzu.common.assist.ResponseResult
     **/
    @ResponseBody
    @RequestMapping("/batchDeleteExamineItemsByItemIds")
    public ResponseResult batchDeleteExamineItemsByItemIds(String examineItemIds) {
        System.out.println("examineItemIds:" + examineItemIds);
        ResponseResult responseResult = examineItemService.batchDeleteExamineItemsByItemIds(examineItemIds);
        return responseResult;
    }


    /**
     * @Author: 张琦[QQ:3098086691]
     * @Description: TODO 使用编号和名称实现‘体检项目’的模糊查询
     * @Date: 2019/12/10 11:39
     * @Param: [searchInput, currentPageNum, pageSize]
     * @Return: cn.edu.lzu.common.assist.DataPage
     **/
    @ResponseBody
    @RequestMapping("/fuzzySearchItemByIdAndName")
    public DataPage fuzzySearchItemByIdAndName(String searchInput, int currentPageNum, int pageSize) {
        System.out.println("searchInput:" + searchInput);
        Session session = SecurityUtils.getSubject().getSession();
        Examiner examiner = (Examiner) ((PrincipalCollection) session.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY)).getPrimaryPrincipal();
        DataPage itemPage = examineItemService.fuzzySearchItemByIdAndName(searchInput, examiner.getUnitId(), currentPageNum, pageSize);
        return itemPage;
    }

    /**
     * @Author: 张琦[QQ:3098086691]
     * @Description: TODO 根据项目类型id获取该单位同为该项目类型的所有项目
     * @Date: 2020/1/15 13:26
     * @Param: [itemTypeId]
     * @Return: java.util.List<cn.edu.lzu.entity.ExamineItem>
     **/
    @ResponseBody
    @RequestMapping("/getItemsListByTypeId")
    public List<ExamineItem> getItemsListByTypeId(Integer itemTypeId) {
        Session session = SecurityUtils.getSubject().getSession();
        Examiner examiner = (Examiner) ((PrincipalCollection) session.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY)).getPrimaryPrincipal();
        List<ExamineItem> examineItemList = examineItemService.getExamineItemsByTypeId(itemTypeId, examiner.getUnitId());
        System.out.println("examineItemList:" + examineItemList);
        return examineItemList;
    }

    /**
     * @Author: 张琦[QQ:3098086691]
     * @Description: TODO 根据id获取体检项目信息
     * @Date: 2020/3/17 16:42
     * @Param: [examineItemId]
     * @Return: cn.edu.lzu.entity.ExamineItem
     **/
    @ResponseBody
    @RequestMapping("/getExamineItemMsg")
    public Map<String, Object> getExamineItemMsg(int examineItemId, int fillItemId) {
        Session session=SecurityUtils.getSubject().getSession();
        Examiner examiner = (Examiner) ((PrincipalCollection) session.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY)).getPrimaryPrincipal();
        Map<String, Object> labExamineClauseList=labExamineClauseService.getLabClauseByItemId(examiner.getUnitId(), examineItemId, fillItemId);
        return labExamineClauseList;
    }

    /**
     * @Author: 张琦[QQ:3098086691]
     * @Description: TODO 根据examineItemId获取examineItem的名称
     * @Date: 2020/3/21 17:09
     * @Param: [examineItemId]
     * @Return: cn.edu.lzu.common.assist.ResponseResult
     **/
    @ResponseBody
    @RequestMapping("/getExamineItemNameById")
    public ResponseResult getExamineItemNameById(Integer examineItemId) {
        ExamineItem examineItem = examineItemService.getExamineItemById(examineItemId);
        ResponseResult responseResult = new ResponseResult();
        responseResult.setMsg(examineItem.getExamineName());
        responseResult.setCode(1);
        return responseResult;
    }

    /**
     * @Author: 张琦[QQ:3098086691]
     * @Description: TODO 获取实验室检查项目
     * @Date: 2020/3/21 20:36
     * @Param: []
     * @Return: java.util.List<cn.edu.lzu.entity.ExamineItem>
     **/
    @ResponseBody
    @RequestMapping("/getLabExamineItems")
    public List<ExamineItem> getLabExamineItems() {
        Session session=SecurityUtils.getSubject().getSession();
        Examiner examiner = (Examiner) ((PrincipalCollection) session.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY)).getPrimaryPrincipal();
        List<ExamineItem> examineItemList=examineItemService.getLabExamineItem(examiner.getUnitId());
        return examineItemList;
    }

    @ResponseBody
    @RequestMapping("/getLabExamineResult")
    public DataPage getLabExamineResult(int examineItemId, int fillItemId){
        Session session=SecurityUtils.getSubject().getSession();
        Examiner examiner = (Examiner) ((PrincipalCollection) session.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY)).getPrimaryPrincipal();
        DataPage dataPage=labExamineClauseService.getLabExamineResult(examiner.getUnitId(), examineItemId, fillItemId);
        System.out.println(dataPage.getData());
        return dataPage;
    }

    @ResponseBody
    @RequestMapping("/getLabExaminerAndTime")
    public Map<String, Object> getLabExaminerAndTime(int fillItemId){
        Map<String, Object> map=examineItemService.getLabExaminerAndTime(fillItemId);
        return map;
    }

}